/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package iot;

import gnu.io.PortInUseException;
import iot.MainFrame;
import static iot.MainFrame.ID_ISOK;
import static iot.MainFrame.mSerialport;
import static iot.MainFrame.mSerialport2;
import static iot.MainFrame.serialPortAliyunString;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author micfirstelec
 */
public class serialthread extends Thread {

    public void run() {
        int iLengDate = 0;
        while (0 == ID_ISOK) {
            try {
                MainFrame.mSerialPortOperate.setText("读取ID中");
                List<String> serialPort = com.yang.serialport.manager.SerialPortManager.findPorts();//重新读取串口
                boolean serialPortStatue = serialPort.contains(MainFrame.serialPortAliyunString);//判读是否有这个串口
                System.out.printf("ID_ISOK=%d\n", ID_ISOK);
                serialPort = SerialPortManager.findPorts();//重新读取串口
                serialPortStatue = serialPort.contains(MainFrame.serialPortAliyunString);//判读是否有这个串口
                //System.out.println(MainFrame.serialPortAliyunString);
                System.out.println("thread...........");
                if (serialPortStatue) {
                    MainFrame.sendData();//发送数据
                    do {
                        System.out.println("in do threading...........");
                        byte[] data = SerialPortManager.readFromPort(MainFrame.mSerialport);//读取串口数据
                        iLengDate = data.length;
                        if (iLengDate > 0) {
                            try {
                                MainFrame.ProcessSerialData(data);
                            } catch (PortInUseException ex) {
                                Logger.getLogger(serialthread.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            try {
                                Thread.sleep(1000 * 1);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(serialthread.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } while (iLengDate <= 0);
                    
                } else {
                    MainFrame.mDataView.append("串口不存在，请重新打开！\r\n");
                }
                Thread.sleep(1000 * 1);
                System.out.printf("ID_ISOK=%d\n", ID_ISOK);
                SerialPortManager.closePort(mSerialport);
                mSerialport = null;
                SerialPortManager.closePort(mSerialport2);
                mSerialport2 = null;
                
                MainFrame.mSerialPortOperate.setText("打开串口");
                ID_ISOK = 1;
                System.out.println("thread end...........");
            } catch (InterruptedException ex) {
                Logger.getLogger(serialthread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
